<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    const throttle = function (fn, interval) {
      let timer = null
      let firstTimer = true
      return function () {
        const _self = this
        const args = arguments
        if (firstTimer) {
          firstTimer = false
          return timer = fn.apply(this, arguments)
        }
        if (timer) {
          return false
        }
        timer = setTimeout(function () {
          clearTimeout(timer)
          timer = null
          fn.apply(_self, args)
        }, interval)
      }
    }

    window.onresize = throttle(() => {
      console.log(window.innerWidth);
    }, 500)
  </script>
</body>

</html>
